<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\DB;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public $guarded = [];


    public function area()
    {
        return $this->belongsTo(Areas::class);
    }

    public function role()
    {
        return $this->belongsTo(Roles::class);
    }

    public function getParentIdAttribute($v)
    {
        if ($v) {
            return $this->getParentName($v, '');
        } else {
            return '';
        }
    }

    protected function getParentName($v)
    {
        $user = DB::table('users')->where('id', $v)->get()[0];
        $parent_id = $user->parent_id;
        $company = $user->company;
        if ($parent_id != 0) {
            $company.='==>'.$this->getParentName($parent_id);
        };
        return $company;
    }

}
